//
//  SEGOldPGCViewController.m
//  SEGUhomeStandard
//
//  Created by 山刘 on 2018/6/28.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGOldPGCViewController.h"
#import "SEGOldPGCListView.h"
#import "SEGMenuListManager.h"

@interface SEGOldPGCViewController ()<UITableViewDelegate,UITableViewDataSource,SEGProtocolOldPGCListViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
/**
 网络请求管理类
 */
@property (nonatomic, strong)SEGMenuListManager *menuListManager;
@property (nonatomic, assign) NSInteger pageNo;
@end

@implementation SEGOldPGCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_bg;
    [self.view addSubview:self.tableView];
    
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        weakSelf.pageNo = 1;
        [weakSelf loadData];
    } footerRefreshBlock:^{
        weakSelf.pageNo += 1;
        [weakSelf loadData];
    }];
    [self.tableView beginHeaderRefreshing];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

#pragma mark - 网络请求
-(void)loadData{
    [SVProgressHUD show];
    [self.menuListManager requestOldPGCListPageNo:self.pageNo PageSize:10 quizTypeId:self.quizTypeId isRecommend:self.isRecommend finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        [SVProgressHUD dismiss];
        if (isSuccessed) {
            [SEGAlertView showRefrshTipsWithMessage:[NSString stringWithFormat:@"为您刷新了%zd条新内容",self.menuListManager.oldPGCFreshCount] parentView:self.tableView];
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.menuListManager.oldPGCDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SEGOldPGCListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SEGOldPGCListViewCellIndetifier];
    if (!cell) cell = [[SEGOldPGCListViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SEGOldPGCListViewCellIndetifier];
    [cell setWithLayout:self.menuListManager.oldPGCDataSource[indexPath.row]];
    cell.varOldPGCListView.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.menuListManager.oldPGCDataSource[indexPath.row].totalHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

-(void)segProtocoOldPGCListViewClick:(SEGPictorialListModel *)sender{
    
    if (sender.type.integerValue == SEGEnumNameObjType_PGC) {
        return;
    }
    
    if (sender.type.integerValue == SEGEnumNameObjType_Topic) {
        return;
    }
    
    NSDictionary* params = [NSDictionary dictionary];
//    if (sender.linkMode.integerValue == SEGEnumLinkModeType_Web) {//网页
//        params = @{@"typeId" : sender.linkMode,
//                   @"href" : sender.linkModules,
//                   @"title" : @""};
//    }else if (sender.linkMode.integerValue == SEGEnumLinkModeType_Image) {//图片
//        params = @{@"typeId" : sender.linkMode,
//                   @"href" : [NSString stringWithFormat:@"%@%@", kClientSmallImageRootURL, sender.linkModules],
//                   @"title" : @""};
//    }else{
//        if([sender.linkModules isEqualToString:@"100999"]||
//           [sender.linkModules isEqualToString:@"100998"]){ //到家服务：100998、到家服务商：100999
//            params = @{@"typeId" : sender.linkModules,
//                       @"href" : sender.url,
//                       @"title" : @""};
//        }else{
//            params = @{@"typeId" : sender.linkModules,
//                       @"href" : sender.linkDetail,
//                       @"title" : @""};
//        }
//    }
    [[SEGUhomeMediator sharedInstance]seg_pushToViewControllerWithType:params];
    return;
}

#pragma mark - getter
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

-(SEGMenuListManager *)menuListManager{
    if (!_menuListManager) {
        _menuListManager = [[SEGMenuListManager alloc]init];
    }
    return _menuListManager;
}

@end
